package by.grsu.verkhovodko.page;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.verkhovodko.app.VerkhovodkoWebApplication;
import by.grsu.verkhovodko.dao.impl.BillDao;
import by.grsu.verkhovodko.dao.impl.TrainDao;
import by.grsu.verkhovodko.entity.Bill;
import by.grsu.verkhovodko.entity.Passenger;
import by.grsu.verkhovodko.entity.Train;

public class BillListPage extends WebPage{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6161816579199150219L;
	private final BillDao billDao = new BillDao(VerkhovodkoWebApplication.DATA_XML_FOLDER);
	
	@SuppressWarnings({ "rawtypes", "serial" })
	public BillListPage(final Passenger passenger) {
		super();
		List<Bill> bills = billDao.getByPassengerId(passenger.getId());
		final TrainDao trainDao = new TrainDao(VerkhovodkoWebApplication.DATA_XML_FOLDER);
		
		add(new Link("mainPage") {
			@Override
			public void onClick() {
				setResponsePage(StartPage.class );
			}
		});
		add(new DataView<Bill>("table-rows", new ListDataProvider<Bill>(bills)) {
			@Override
			protected void populateItem(final Item<Bill> item) {
				final Bill bill = item.getModelObject();
				Train train = trainDao.get(bill.getTrainId());
				
				item.add(new Label("id", bill.getId()));
				item.add(new Label("toStation",train.getDestStation()));
				item.add(new Label("passengerName", passenger.getLogin()));
				item.add(new Label("cost", train.getCost()));
				item.add(new Label("isPayed", bill.isPayed()));
					
				item.add(new Link("payLink") {
					@Override
					public void onClick() {
						bill.setPayed(true);
						billDao.update(bill);
						setResponsePage(new BillListPage(passenger));
					}
				});
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						billDao.delete(bill.getId());
						setResponsePage(new BillListPage(passenger));
					}
				});
			}
		});
		
	}

}
